package com.qf.hotel.controller;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

@WebServlet(name = "BaseServlet02")
public class BaseServlet02 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置字符编码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        try {
            // 获取请求方法method携带的数据
            String methodName = request.getParameter("method");
            System.out.println("methodName==" + methodName);

            // 使用反射创建请求的Servlet的对象
            Class<? extends BaseServlet02> clazz = this.getClass();

            // 使用反射获取方法
            Method method = clazz.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);

            // 调用方法
            Object obj = method.invoke(this, request, response);
            System.out.println("obj===" + obj);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
